/*
 * @Author: your name
 * @Date: 2021-11-26 15:24:47
 * @LastEditTime: 2021-11-29 17:56:14
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \typescript_reptile\src\crowller.ts
 */
// npm isntall superagent --save
// npm install cheerio --save
// 爬虫工具 ts -> .d.ts 翻译文件 @types/superagent -> ts
import fs from 'fs';
import path from 'path';
import superagent from 'superagent';
import DellAnalyzer from './dellAnalyzer';
// import LessAnalyzer from './lessAnalyzer';

export interface Analyzer {
  analyze: (html: string, filePath: string) => string;
}

class Crowller {
  private filePath = path.resolve(__dirname, '../data/course.json');

  // 获取页面内容
  private async getRawHtml() {
    const result = await superagent.get(this.url);
    return result.text;
  }
  // 写入内容
  private writeFle(content: string) {
    fs.writeFileSync(this.filePath, content);
  }
  // 获取html标签，将得到的内容写入文件
  private async initSpiderProcess() {
    const html = await this.getRawHtml();
    const fileContent = this.analyzer.analyze(html, this.filePath);
    this.writeFle(fileContent);
  }

  constructor(private url: string, private analyzer: Analyzer) {
    this.initSpiderProcess();
  }
}

const secret = 'x3b174jsx';
const url = `http://www.dell-lee.com/typescript/demo.html?secret=${secret}`;

const analyzer = DellAnalyzer.getInstance();
// const analyzer = new DellAnalyzer();
// const analyzer = new LessAnalyzer();
new Crowller(url, analyzer);
